package session09;

import ATM.FileOperation;
import ATM.UserInfo;
import session09.compent.FontPanel;
import session09.compent.JButtonPanel;
import session09.compent.PanePanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TakePanel extends PanePanel {
    private JButtonPanel clearBtn;
    private JButtonPanel exitBtn;
    private JButtonPanel sureBtn;

    private FontPanel money;
    private JTextField txt;

    public TakePanel(ATMFrame frame) {
        super(200, 250, 180);

        this.clearBtn = new JButtonPanel("image/buttonClear.jpg", 50, 450);
        this.clearBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TakePanel.this.txt.setText("");
            }
        });
        this.exitBtn = new JButtonPanel("image/buttonOff.jpg", 442, 550);
        this.exitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Container atm = frame.getContentP();
                CardLayout cardLayout = (CardLayout) atm.getLayout();
                cardLayout.show(atm, "select");
            }
        });
        this.sureBtn = new JButtonPanel("image/buttonSure.jpg", 832, 450);
        this.sureBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String money = TakePanel.this.txt.getText();
                Double inputMoney = Double.parseDouble(money);
                UserInfo user = frame.getLogin().login();
                user.setAccount(user.getAccount() - inputMoney);
                frame.getLogin().setCash((int) (frame.getLogin().getCash() - inputMoney));
                frame.getQueryPanel().getTxt1().setText(user.getAccount() + "");
                FileOperation.writeFile(frame.getLogin().getAllUsers());
                JOptionPane.showMessageDialog(frame,"取钱成功");
            }
        });
        this.getBackLab().add(this.clearBtn);
        this.getBackLab().add(this.exitBtn);
        this.getBackLab().add(this.sureBtn);

        this.money = new FontPanel(60, 80, 20, 150, 30);
        this.money.setText("请输入取款金额");
        this.getImgLab().add(this.money);
        this.txt = new JTextField();
        this.txt.setBounds(230, 80, 160, 30);
        this.getImgLab().add(this.txt);
    }
}
